/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client;

import javax.microedition.lcdui.AlertType;
import javax.microedition.rms.RecordStoreException;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.core.KuixConverter;
import org.kalmeo.kuix.core.KuixMIDlet;
import org.kalmeo.kuix.widget.Desktop;
import org.kalmeo.kuix.widget.Screen;
import org.kalmeo.kuix.widget.TextWidget;
import org.kalmeo.kuix.widget.Widget;
import org.mobiubiq.directory.client.frame.MainScreenFrame;
import org.mobiubiq.directory.client.widget.AudioPlayer;
import org.mobiubiq.directory.client.widget.BarcodeReaderWidget;
import org.mobiubiq.directory.client.widget.Camera;
import org.mobiubiq.directory.client.widget.Form;
import org.mobiubiq.directory.client.widget.Image;
import org.mobiubiq.directory.client.widget.Submit;
import org.mobiubiq.directory.client.widget.WaitWidget;

/**
 *
 * @author Marnix Renne
 */
public class KuixClient extends KuixMIDlet {
    private static boolean debug = false;
    private static Screen processingScreen;
    private static Thread processingThread;
    
    public KuixClient() throws RecordStoreException {
        super();
        String isDebug = KuixMIDlet.getDefault().getAppProperty("debug");
        if("true".equalsIgnoreCase(isDebug)) {
            debug = true;
        }
    }
    
    public void initDesktopStyles() {
    }

    public void initDesktopContent(Desktop desktop) {
        Kuix.loadCss("mobiubiq.css");
        Kuix.getFrameHandler().pushFrame(new MainScreenFrame());
    }

    protected KuixConverter createNewConverterInstance() {
        return new KuixConverter() {
            public Widget convertWidgetTag(String tag) {
                if(BarcodeReaderWidget.BARCODE_READER_WIDGET_TAG.equals(tag)) {
                    return new BarcodeReaderWidget();
                }
                if(WaitWidget.WAIT_WIDGET_TAG.equals(tag)) {
                    return new WaitWidget();
                }
                if(Image.IMAGE_TAG.equals(tag)) {
                    return new Image();
                }
                if(Form.FORM_WIDGET_TAG.equals(tag)) {
                    return new Form();
                }
                if(Submit.SUBMIT_WIDGET_TAG.equals(tag)) {
                    return new Submit();
                }
                if(Camera.CAMERA_WIDGET_TAG.equals(tag)) {
                    return new Camera();
                }
                if(AudioPlayer.AUDIOPLAYER_WIDGET_TAG.equals(tag)) {
                    return new AudioPlayer();
                }
                return super.convertWidgetTag(tag);
            }
        };
    }
    
    public static void debug(String message) {
        if(debug) {
            Kuix.alert(message);
        }
    }
    
    public static void debug(String message, int options) {
        if(debug) {
            Kuix.alert(message, options);
        }
    }
    
    public static boolean isDebug() {
        return debug;
    }
    
    public static void showProcessingScreen(String message) {
        if(processingScreen == null) {
            processingScreen = Kuix.loadScreen("processing.xml", null);
        }
        TextWidget textWidget = (TextWidget) processingScreen.getWidget("message");
        textWidget.setText(message);
        processingScreen.setCurrent();
    }
    
    public static void cleanupProcessingScreen() {
        if(processingScreen != null) {
            processingScreen.cleanUp();
            processingScreen = null;
        }
    }
//    public static PropertyRecordStore getSettings() {
//        return KuixClient.settings;
//    }
}
